package com.mengxuegu.springboot.service;

import com.mengxuegu.springboot.mybatisplus.entities.Bill;
import com.mengxuegu.springboot.mybatisplus.entities.User;
import com.mengxuegu.springboot.mybatisplus.service.IBillService;
import com.mengxuegu.springboot.mybatisplus.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

@Service
public class BizBillService {
    @Autowired
    private IUserService userService;

    @Autowired
    private IBillService billService;

    @Autowired
    private BizBillService bizBillService;

    public void addUseBill(User user, Bill bill) {
//        bizBillService.addUserAndBill(user, bill);  // 事务会执行
//        addUserAndBill(user, bill);  // 事务不会执行
    }

    @Transactional
    public void addUserAndBill(User user, Bill bill) throws SQLException{
        boolean save1 = billService.save(bill);
        if (user.getRealName().equals("李四")) {
            throw new SQLException();
        }
        boolean save = userService.save(user);
//        int a = 1/0;
        System.out.println(save);
        System.out.println(save1);
    }
}
